﻿using IRepository.SystemDefault;
using Model.SystemDefault;
using System;
using System.Threading;
using System.Linq;
using Service;
using System.Threading.Tasks;
using System.Collections.Generic;
using Common.Net;
using System.Reflection;
using OpenInterface;
using System.Text;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace Crawler
{
    /// <summary>
    /// 临时项目，应付临时使用
    /// </summary>
    class Program
    {
       
        private static IAnalyerConfigRepository analyerConfigRepository = null;
        private static IUrlCheckerConfigRepository urlCheckerConfigRepository = null;
        private static ICollectorConfigRepository collectorConfigRepository = null;
        private static ISetupConfigRepository setupConfigRepository = null;
        private static ITerminalConfigRepository terminalConfigRepository = null;

        private static TerminalConfigModel terminalConfig = null;
        private static SetupConfigModel setupConfigModel = null;
        private static UrlCheckerConfigModel urlCheckerConfigModel = null;
        private static CollectorConfigModel collectorConfigModel = null;
        private static AnalyerConfigModel analyerConfigModel = null;

        private static IConfiguration config = null;
        private static BaseTerminalConfig baseTerminalConfig = null;

        static void Main(string[] args)
        {
            //载入客户端配置信息
            var builder = new ConfigurationBuilder();
            config = builder.SetBasePath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "config"))
                .AddJsonFile("config.json", true, true)
                .Build();

            baseTerminalConfig = config.Get<BaseTerminalConfig>();

            Console.WriteLine("Hello World!");
            ///注册支持中文
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            InitRepository();
            InitConfig();

            CrawlerService crawlerService = new CrawlerService();

            crawlerService.Init();

            Task.Factory.StartNew(() => crawlerService.SetUp(setupConfigModel));
            Task.Factory.StartNew(() => crawlerService.UrlChecker(urlCheckerConfigModel));
            Task.Factory.StartNew(() => crawlerService.Collector(collectorConfigModel));
            Task.Factory.StartNew(() => crawlerService.Analyzer(analyerConfigModel));

            while (!Console.ReadLine().Equals("exit")) Thread.Sleep(200);
        }
        /// <summary>
        /// 初始化仓库
        /// </summary>
        private static void InitRepository()
        {
            analyerConfigRepository = ServiceManager.GetService<IAnalyerConfigRepository>();
            urlCheckerConfigRepository = ServiceManager.GetService<IUrlCheckerConfigRepository>();
            collectorConfigRepository = ServiceManager.GetService<ICollectorConfigRepository>();
            setupConfigRepository = ServiceManager.GetService<ISetupConfigRepository>();
            terminalConfigRepository = ServiceManager.GetService<ITerminalConfigRepository>();

        }
        /// <summary>
        /// 初始化采集配置
        /// </summary>
        private static void InitConfig()
        {
            //定时更新配置
            Task.Factory.StartNew(()=>
            {
                while(true)
                {
                    terminalConfig = terminalConfigRepository.Find(baseTerminalConfig.TerminalID, baseTerminalConfig.ConstrConfigData);
                    if (terminalConfig == null) return;

                    setupConfigModel = setupConfigRepository.Find(terminalConfig.SetupConfigID, baseTerminalConfig.ConstrConfigData);

                    urlCheckerConfigModel = urlCheckerConfigRepository.Find(terminalConfig.UrlCheckerConfigID, baseTerminalConfig.ConstrConfigData);

                    collectorConfigModel = collectorConfigRepository.Find(terminalConfig.CollectConfigID, baseTerminalConfig.ConstrConfigData);

                    analyerConfigModel = analyerConfigRepository.Find(terminalConfig.AnalyerConfigID, baseTerminalConfig.ConstrConfigData);
                    Thread.Sleep(2000);
                }

            });

        }

        
    }
}
